package com.llt.controller;


import com.llt.annotation.UserLoginToken;
import com.llt.enum_util.ResponseBodyEnum;
import com.llt.pojo.ClassAll;
import com.llt.pojo.ClassFirst;
import com.llt.pojo.ClassSecond;
import com.llt.pojo.common.AjaxResponseBody;
import com.llt.service.ClassFirstService;
import com.llt.service.ClassSecondService;
import com.llt.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@CrossOrigin
@Api(value = "分类",tags = {"分类"})
public class ClassController {
    @Autowired
    private ClassFirstService classFirstService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ClassSecondService classSecondService;
    @PostMapping("/addClassSecond")
    @ResponseBody
    @UserLoginToken
    @ApiOperation(value = "添加二级分类",response = ClassFirst.class)
    public AjaxResponseBody addClassSecond(@Validated @RequestBody ClassSecond classSecond){
        ClassSecond classSecond1 = classSecondService.queryByName(classSecond.getName());
            if (classSecond1 !=null){
                return  new AjaxResponseBody(ResponseBodyEnum.RESPONSE_BODY_CLASS_ERROR_HAVE) ;
            }
        int i = classSecondService.addClassSecond(classSecond);
        if (i==1){
            redisUtil.del("classAll");
            return  new AjaxResponseBody(ResponseBodyEnum.RESPONSE_BODY_ENUM_OK) ;
        }
        return  new AjaxResponseBody(ResponseBodyEnum.RESPONSE_BODY_SYSTEM_ERROR) ;

    }
    @GetMapping("/classSecondSelectByFirstIDAll")
    @ResponseBody
    @UserLoginToken
    @ApiOperation("获取全部二级分类")
    public  AjaxResponseBody classSecondSelectByFirstIDAll(Integer id){

        List<ClassSecond> classSecond = classSecondService.classSecondSelectByFirstIDAll(id);
        return  new AjaxResponseBody(ResponseBodyEnum.RESPONSE_BODY_ENUM_OK,classSecond) ;
    }
    @PostMapping("/updateClassSecond")
    @UserLoginToken
    @ResponseBody
    @ApiOperation(value = "更新二级分类")
    public AjaxResponseBody updateClassSecond(@Validated @RequestBody ClassSecond classSecond){
        classSecondService.updateClassSecond(classSecond);
        redisUtil.del("classAll");
        return  new AjaxResponseBody(ResponseBodyEnum.RESPONSE_BODY_ENUM_OK) ;
    }
    @GetMapping("/deleteClassSecond")
    @UserLoginToken
    @ResponseBody
    @ApiOperation(value = "删除二级分类")
    public AjaxResponseBody deleteClassSecond(@RequestParam("id") Integer id){
        Integer data1 = classSecondService.deleteClassSecond(id);
        if (data1==0){
            return  new AjaxResponseBody(ResponseBodyEnum.RESPONSE_BODY_SYSTEM_ERROR) ;
        }
        redisUtil.del("classAll");
        return  new AjaxResponseBody(ResponseBodyEnum.RESPONSE_BODY_ENUM_OK) ;
    }
    @PostMapping("/addClassFirst")
    @UserLoginToken
    @ResponseBody
    @ApiOperation(value = "添加一级分类")
    public AjaxResponseBody addClassFirst(@Validated @RequestBody ClassFirst classFirst){
        ClassFirst i11 = classFirstService.queryByName(classFirst.getName());
        if (i11 !=null){
            return  new AjaxResponseBody(ResponseBodyEnum.RESPONSE_BODY_CLASS_ERROR_HAVE) ;
        }
        int i = classFirstService.addClassFirst(classFirst);
        if (i==1){
            return  new AjaxResponseBody(ResponseBodyEnum.RESPONSE_BODY_ENUM_OK) ;
        }
        return  new AjaxResponseBody(ResponseBodyEnum.RESPONSE_BODY_SYSTEM_ERROR) ;
    }
    @PostMapping("/updateClassFirst")
    @UserLoginToken
    @ResponseBody
    @ApiOperation(value = "更新一级分类")
    public AjaxResponseBody updateClassFirst(@Validated @RequestBody ClassFirst classFirst){
        ClassFirst data1 = classFirstService.queryByName(classFirst.getName());
        Integer i;
        if (data1==null){
             i = classFirstService.updateClassFirst(classFirst);
        }else {
            return  new AjaxResponseBody(ResponseBodyEnum.RESPONSE_BODY_CLASS_ERROR_HAVE) ;
        }
        return  new AjaxResponseBody(ResponseBodyEnum.RESPONSE_BODY_ENUM_OK,i) ;
    }
    @GetMapping("/deleteClassFirst")
    @UserLoginToken
    @ResponseBody
    @ApiOperation(value = "删除一级分类")
    public AjaxResponseBody deleteClassFirst(@RequestParam("id") Integer id){

        Integer data1 = classFirstService.deleteClassFirst(id);
        if (data1==0){
            return  new AjaxResponseBody(ResponseBodyEnum.RESPONSE_BODY_SYSTEM_ERROR) ;
        }
        return  new AjaxResponseBody(ResponseBodyEnum.RESPONSE_BODY_ENUM_OK) ;
    }
    @GetMapping("/classFirstSelectAll")
    @ResponseBody
    @UserLoginToken
    @ApiOperation("获取全部一级分类")
    public  AjaxResponseBody classFirstSelectAll(){
        List<ClassFirst> classFirsts = classFirstService.classFirstSelectAll();
        return  new AjaxResponseBody(ResponseBodyEnum.RESPONSE_BODY_ENUM_OK,classFirsts) ;
    }
    @GetMapping("/classAll")
    @ResponseBody
    @ApiOperation("获取全部分类")
    public  AjaxResponseBody classAll(){
                List<Object> classAll = redisUtil.range("classAll",0,-1);
        if (classAll!=null && classAll.size()>0){
            return    new AjaxResponseBody(ResponseBodyEnum.RESPONSE_BODY_ENUM_OK,classAll);
        }
        List<ClassAll> classFirsts = classFirstService.classAll();

        try {
            redisUtil.lSet("classAll",classFirsts);
        }catch (RuntimeException e){
            e.printStackTrace();
        }

        return  new AjaxResponseBody(ResponseBodyEnum.RESPONSE_BODY_ENUM_OK,classFirsts) ;
    }

}
